package Top200;

/**
 * @author zhangmin
 * @create 2022-02-21 17:11
 * 递归
 */
public class countAndSay38 {
    public String countAndSay(int n) {
        if (n==1) return "1";
        String pre = countAndSay(n - 1);
        int count=1;
        char cur=pre.charAt(0);
        StringBuffer sb=new StringBuffer();
        for (int i = 1; i < pre.length(); i++) {
            if (pre.charAt(i)!=cur){
                //前一个字符的个数出现
                sb.append(count).append(cur);
                cur=pre.charAt(i);
                count=1;
            }else {
                count++;
            }
        }
        sb.append(count).append(cur);
        return sb.toString();
    }
}
